<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <div class="finish">
                <div class="finishNum">总欠款:
                    <div class="text">{{ total_stock.money }}</div>
                </div>
                <div class="finishNum">已付总金额:
                    <div class="text">{{ total_stock.pay_money }}</div>
                </div>
                <div class="finishNum">应付总金额:
                    <div class="text">{{ total_stock.only_money }}</div>
                </div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                <el-select v-model="search.supplier_id" placeholder="请选择供应商" filterable clearable>
                    <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                </el-select>
                <el-date-picker v-model="search.business_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期"
                                end-placeholder="结束日期"/>
                <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border @setExtend="setExtend">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <el-table-column label="供应商" prop="supplier.apply_name" align="center"></el-table-column>
                <el-table-column label="联系人" prop="supplier.linkman" align="center"></el-table-column>
                <el-table-column label="联系电话" prop="supplier.mobile" align="center"></el-table-column>
                <el-table-column label="欠款总额" prop="all_money" align="center"></el-table-column>
                <el-table-column label="已付欠款" prop="all_pay_money" align="center"></el-table-column>
                <el-table-column label="应付欠款" prop="all_only_money" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                        <el-button text type="primary" size="small" @click="table_details(scope.row, scope.$index)" v-auth="'payable.detail'">欠款详情</el-button>
                        <el-button text type="warning" size="small" @click="table_fu(scope.row, scope.$index)" v-auth="'payable.payment'">付款</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <details-dialog v-if="dialog.detail" ref="detailDialog" @success="handleSuccess" @closed="dialog.detail = false" :query="{supplier_id:this.supplier_id,scene:1}"></details-dialog><!-- 欠款详情 -->
    <payment v-if="dialog.payment" ref="paymentDialog" @success="handleSuccess" @closed="dialog.payment = false"></payment>
</template>

<script>
import payment from '../payment/save.vue'
import detailsDialog from './details.vue'

export default {
    name: '应付账款（列表）',
    components: {
        detailsDialog, payment,
    },
    data() {
        return {
            apiObj: this.$API.finance.payable.list,
            selection: [],
            search: {
                supplier_id: null,
                business_date: null,
            },
            dialog: {
                payment: false,
                detail: false,
            },
            supplier_id: "",
            applyList: [],
            total_stock: {},
        }
    },
    mounted() {
        this.getApply()
    },
    methods: {
        setExtend(data) {
            this.total_stock = data
        },
        /**欠款详情*/
        table_details(row) {
            this.supplier_id = row.supplier_id
            this.dialog.detail = true
            this.$nextTick(() => {
                this.$refs.detailDialog.open('show')
            })
        },
        //付款
        table_fu(row) {
            this.dialog.payment = true
            this.$nextTick(() => {
                this.$refs.paymentDialog.open('payment').paymentData(row)
            })
        },
        //删除
        async table_del(row, index) {
            var reqData = {id: row.id}
            var res = await this.$API.finance.account.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", {type: 'error'})
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })
            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(async () => {
                var reqData = {id: delete_ids}
                var res = await this.$API.finance.account.delete.post(reqData);
                if (res.code == 1) {
                    this.$refs.table.refresh()
                    this.$message.success("删除成功")
                } else {
                    this.$alert(res.message, "提示", {type: 'error'})
                }
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
        this.selection = selection;
        },
        //搜索
        upsearch() {
        this.$refs.table.upData(this.search)
        },
        refresh() {
        for (const key in this.search) {
            this.search[key] = null;
        }
        this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
        if (mode == 'add') {
            this.$refs.table.refresh()
        } else if (mode == 'edit') {
            this.$refs.table.refresh()
        }
        },
        // 供应商
        async getApply() {
            var res = await this.$API.apply.apply.all.get();
            this.applyList = res.data;
        },
    }
}
</script>

<style></style>
